/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.util;

import java.awt.Container;
import java.io.File;

import javax.swing.JFileChooser;

import org.apache.log4j.Logger;

public class ThematicMapPropertiesUIHelper extends BaseUIHelper {
	
    private static Logger logger = Logger.getLogger(ThematicMapPropertiesUIHelper.class);	

    private File openFile( Container container, String[] ext, File dir ) {
		File file =null;
		FileChooserFilter defaultFcf =null;
		
	    JFileChooser fileChooser = new JFileChooser();
	    for( int ct=0; ct < ext.length; ct++ ) {
	    	FileChooserFilter fcf =new FileChooserFilter( ext[ ct ] );
	    	if ( ct == 0 ) {
	    		defaultFcf =fcf;
	    	}
	    	fileChooser.addChoosableFileFilter( fcf );
	    }
	    fileChooser.setCurrentDirectory( getTMapDir( container, dir ) );
	    fileChooser.setFileFilter( defaultFcf );
	    
		int retVal =fileChooser.showOpenDialog(container);
		
		if ( retVal == JFileChooser.APPROVE_OPTION ) {		
		    file =fileChooser.getSelectedFile();	
		    logger.debug( "fileName=" + file.getAbsolutePath() );
		}
		
		return file;    	
    }
    
    private File openFile( Container container, String ext, File dir ) {
    	return openFile( container, new String[] { ext }, dir );
    }
    
	public File getBaseMapFile( Container container ) {
		return openFile( container
					   , new String[] { FileChooserFilter.BASE_MAP_EXT, FileChooserFilter.SHAPE_FILE_EXT }
					   , FileChooserFilter.MAPS_DIR );
	}
	
	public File getDataFile( Container container ) {
		return openFile( container
					   , new String[] { FileChooserFilter.DATA_EXT, FileChooserFilter.DBF_EXT
									  , FileChooserFilter.EXCEL_EXT, FileChooserFilter.MDB_EXT }
					   , FileChooserFilter.DATA_DIR ); 
	}
	
	public File getCartographicDisplayFile( Container container ) {
		return openFile( container,  FileChooserFilter.CARTOGRAPHIC_DISPLAY_EXT, FileChooserFilter.CDS_DIR );		
	}	
	
	public File getAnnotationsFile( Container container ) {
		return openFile( container,  FileChooserFilter.ANNOTATIONS_EXT, FileChooserFilter.ANNOTATIONS_DIR );		
	}		
	
	public File getBackgroundImageFile( Container container ) {
		return openFile( container,  new String[] { FileChooserFilter.GIF_EXT, FileChooserFilter.JPG_EXT, FileChooserFilter.PNG_EXT }, FileChooserFilter.ANNOTATIONS_DIR );			
	}
}
